<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <style>
        #app {
            padding: 50px;
            text-align: center;
        }

        #result {
            margin: 20px auto 0;
            word-break: break-all;
        }
    </style>
</head>

<body>
    <div id="app">
        <textarea id="content" placeholder="输入COS图片地址，如https://example-1250000000.cos.ap-chongqing.myqcloud.com/example.jpg，图片支持格式：PNG、JPG、JPEG" rows="5" cols="50"></textarea>
        <div>
            <button id="btn1">车辆车牌检测</button>
        </div>
        <div id="result">
        </div>
    </div>
</body>

    <script src="../common/xml2json.js"></script>
    <script type="text/javascript">
        document.querySelector('#btn1').onclick = function () {
            let url = document.querySelector('#content').value.trim();

            if (url === '') {
                return alert('输入COS图片地址')
            }

            // 请求签名，详情请参见文档 https://cloud.tencent.com/document/product/436/7778
            const sign = 'q-sign-algorithm=xxx'

            url = url + '?ci-process=DetectCar&' + sign;
            
            const xhr = new XMLHttpRequest();
            xhr.open('get', url);
            xhr.send();
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4 && xhr.status === 200) {
                    const data = xml2js(xhr.responseXML);
                    const info = data?.Response?.CarTags || [];
                    const cars = info.sort((a,b) => Number(a.Confidence) <= Number(b.Confidence) ? 1 : -1)
                                    .map((item, index) => {
                                        return `车辆${index+1}（置信度${item.Confidence}）：` 
                                            + `${item.Serial} ${item.Brand} ${item.Color}`
                                    });
                    document.querySelector('#result').innerText = cars.join('\n')
                }
            }
        }
    </script>

</html>
